const UserService = require("../../services/admin/UserService")
const JWT = require("../../util/JWT")



const UserController = {
    // 拿到前端数据拆分等操作
    login: async (req, res) => {
        // 查询工作在service
        let result = await UserService.login(req.body)
        // console.log(result);
        if (result.length === 0) {
            res.send({
                code: '-1',
                error: "用户不存在或密码错误，请重新输入"
            })
        } else {
            // 生成token
            const token = JWT.generate({
                _id: result[0]._id,
                username: result[0].username
            }, "1d")
            res.header("Authorization", token)
            res.send({
                ActionType: 'OK',
                data: {
                    username: result[0].username,
                    gender: result[0].gender ? result[0].gender : 0, // 性别 利用数字 0 1 2 
                    introduction: result[0].introduction,// 简介
                    avatar: result[0].avatar, // 头像
                    role: result[0].role //管理员1 ，编辑2
                }
            })
        }

    },
    upload: async (req, res) => {
        // console.log(req.body, req.file);
        const token = req.headers["authorization"].split(" ")[1]
        const payload = JWT.verify(token)
        const { username, introduction, gender } = req.body
        // const result = await UserService.getAvatar({ _id: payload._id })
        const avatar = req.file ? `/avataruploads/${req.file.filename}` : ''

        // console.log(payload._id);

        // 调用service模块更新数据
        await UserService.upload({ _id: payload._id, username, introduction, gender: Number(gender), avatar })
        if (avatar) {
            res.send({
                ActionType: "OK",
                data: {
                    username, introduction, gender: Number(gender), avatar
                }
            })
        } else {
            res.send({
                ActionType: "OK",
                data: {
                    username, introduction, gender: Number(gender)
                }
            })
        }

    },
    add: async (req, res) => {
        const { username, password, role, gender, introduction } = req.body
        const avatar = req.file ? `/avataruploads/${req.file.filename}` : ''
        // 调用service模块更新数据
        try {
            await UserService.add({ username, password, role: Number(role), introduction, gender: Number(gender), avatar })
            res.send({
                ActionType: "OK"
            })
        } catch (error) {
            res.send({ errorMsg: "添加用户失败，用户名已存在" });
        }

    },
    getList: async (req, res) => {
        const result = await UserService.getList(req.params)
        res.send({
            ActionType: "OK",
            data: result
        })
    },
    delList: async (req, res) => {
        try {
            await UserService.delList({ _id: req.params.id })
            res.send({
                ActionType: "OK"
            })
        } catch (error) {
            res.send({ errorMsg: "删除用户失败" });
        }

    },
    putList: async (req, res) => {
        try {
            const result = await UserService.putList(req.body)
            res.send({
                ActionType: "OK"
            })
        } catch (error) {
            res.send({ errorMsg: "更新用户信息失败，请检查填写用户名是否重复！" });
        }
    }
}

module.exports = UserController